package com.readfeed.app.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.readfeed.app.R;
import com.readfeed.app.model.Feed;

public class FeedsListAdapter extends ArrayAdapter<Feed> {
	private List<Feed> mDisplayFeeds;
	private List<Feed> mSelectedFeeds;
	private SparseBooleanArray mSelection;
	private Activity mContext;
	
	public FeedsListAdapter(Activity context, int resourceId, List<Feed> feeds) {
		super(context, resourceId, feeds);
		this.mDisplayFeeds = feeds;		
		this.mContext = context;
		this.mSelection = new SparseBooleanArray();
		this.mSelectedFeeds = new ArrayList<Feed>();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		String datePattern = mContext.getResources().getString(R.string.pubdate_format_pattern);		
		
		if (convertView == null) {
			
			LayoutInflater inflater = mContext.getLayoutInflater();
			convertView = inflater.inflate(R.layout.feed_row, null);
			
			viewHolder = new ViewHolder();
			viewHolder.feedTitle = (TextView) convertView.findViewById(R.id.feed_title);
			viewHolder.feedPubDate = (TextView) convertView.findViewById(R.id.pubdate_text);
			viewHolder.selectedIcon = (ImageView) convertView.findViewById(R.id.feed_selected_icon);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		Feed feed = this.mDisplayFeeds.get(position);
		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.US);
		String date = df.format(feed.getPubDate());
		if (feed != null) {
			viewHolder.feedTitle.setText(feed.getTitle());
			viewHolder.feedPubDate.setText(date);
		}
		
		if (mSelection.get(position)) {
			viewHolder.selectedIcon.setVisibility(View.VISIBLE);
		} else {
			viewHolder.selectedIcon.setVisibility(View.INVISIBLE);
		}
		
		return convertView;		
	}
	
	public void removeFeed(int pos){
		this.mDisplayFeeds.remove(pos);
		notifyDataSetChanged();
	}
	
	public Feed getFeedByPosition(int position){
		return mDisplayFeeds.get(position);
	}
	
	public List<Feed> getSelectedFeeds(){
		return mSelectedFeeds;
	}
	
	public void removeSelectedFeeds(){
		for (int i = 0; i < mSelectedFeeds.size(); i++) {
			Feed feedToRemove = mSelectedFeeds.get(i);
			mDisplayFeeds.remove(feedToRemove);
		}
		
		notifyDataSetChanged();
	}
	
	public void setNewSelection(int position, boolean value){
		this.mSelection.put(position, value);
		Feed feed = mDisplayFeeds.get(position);
		mSelectedFeeds.add(feed);
		notifyDataSetChanged();
	}
	
	public boolean isPositionChecked(int position){
		Boolean result = this.mSelection.get(position);
		if (result == null) {
			return false;
		}
		
		return result;
	}
	
	public void removeSelection(int position){
		this.mSelection.delete(position);
		Feed feed = mDisplayFeeds.get(position);
		mSelectedFeeds.remove(feed);
		notifyDataSetChanged();
	}
	
	public void clearSelection(){
		this.mSelection = new SparseBooleanArray();
		mSelectedFeeds = new ArrayList<Feed>();
		notifyDataSetChanged();
	}
	
	private static class ViewHolder{
		TextView feedTitle;
		TextView feedPubDate;
		ImageView selectedIcon;
	}
}
